package Model.Util;

import java.sql.ResultSet;
import java.sql.SQLException;

import Model.Bean.Phone;
import Model.DbAccess.DbConnection;

public class AdminAction {
	
	DbConnection db = new DbConnection();
	
	/*
	 * Admin login
	 */
	public String Login(String adminID, String password) throws SQLException 
	{
		
		if(adminID.equals("")) return "Please type ID.";
		if(password.equals("")) return "Please type password.";
		
		String sql = "Select * from tbAdmin where AdminID = ? and [Password] = ?";
		
		String[] params = new String[] {
			adminID, password	
		};
		
		ResultSet rs = db.executeQuery(sql, params);
		
		if(!rs.next()) {
			return "Login fail. ID or password is incorrect !";
		}
		
		return "done";
	}
	
	
	/*
	 * Add New Phone
	 */
	public boolean AddPhone(Phone phone)
	{
		String sql = "Insert into * values(?,?,?,?)";
		
		Object[] params = new Object[] {
				phone.getPhoneName(),
				phone.getPhoneType(),
				phone.getDescription(),
				phone.getPrice()
			};
		
		boolean rs = db.executeUpdate(sql, params);
		
		return rs;
	}
	
	
	/*
	 * Edit Phone
	 */
	public boolean EditPhone(Phone phone)
	{
		String sql = "Update Phone " +
				"Set PhoneName = " + phone.getPhoneName() +
				", PhoneType = " + phone.getPhoneType() + 
				", Description = " + phone.getDescription() +
				", Price = " + phone.getPrice() +
				" Where PhoneID = " + phone.getPhoneID();
		
		Object[] params = new Object[] {
				phone.getPhoneName(),
				phone.getPhoneType(),
				phone.getDescription(),
				phone.getPrice()
			};
		
		boolean rs = db.executeUpdate(sql, params);
		
		return rs;
	}
}
